package com.demo2do.fms.admin.entity.report.data;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import org.springframework.jdbc.core.RowMapper;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

/**
 *
 * @author Ark
 *
 */
public class VehicleReportSearch implements QueryBuilder {
    
    /** 所属公司 */
    private List<Long> organizationIds;
    
    /** 驳运车代码 */
    private String code;
    
    /** 驳运车牌照 */
    private String barCode;
    
    /** 动力类型 */
    private Long  vehicleFeatureId;
    
    /** 车队id */
    private Long teamId;
    
    /** 累计行驶公里数 - start */
    private BigDecimal beginDistance;
    
    /** 累计行驶公里数 - end */
    private BigDecimal endDistance;
    
    /** 导出条数上限 */
    private Integer limit;
    
    private QueryFilter filter;
    
    private QueryOrder order;
    
    /**
     * The default constructor
     */
    public VehicleReportSearch() {
        order = new QueryOrder();
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        
        if(filter == null || refresh) {

            filter = new QueryFilter().addSentence("v.organization_id IN (:organizationIds)", "organizationIds", organizationIds)
                                      .addLike("v", "vehicle_code", code)
                                      .addLike("v", "vehicle_bar_code", barCode)
                                      .addEquals("v", "vehicle_feature_id", vehicleFeatureId)
                                      .addEquals("v", "truck_team_id", teamId)
                                      .addGreaterThan("v", "distance", beginDistance)
                                      .addLessThan("v", "distance", endDistance);
            
        }
        
        return filter;
         
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        
        StringBuffer buffer = new StringBuffer(256);
        
        buffer.append("SELECT v.organization_name AS organizationName, ")
                     .append("v.vehicle_code AS vehicleCode, ")
                     .append("v.vehicle_bar_code AS vehicleBarCode, ")
                     .append("v.vehicle_feature_name AS vehicleFeatureName, ")
                     .append("(SELECT NAME FROM tbl_truck_team_info WHERE id = v.truck_team_id AND is_disabled = 0) AS teamName, ")
                     .append("v.distance AS distance ")
              .append("FROM vw_vehicle v WHERE v.is_disabled = 0 " + getPreparedFilter(false).clauseSentence());
        
        String sentence = "SELECT * FROM (" + buffer.toString() + ") WHERE ROWNUM <=" + limit;
        return sentence;
        
    }
    
    /**
     * build rowMapper
     * 
     * @return
     */
    public RowMapper<Map<String, Object>> buildRowMapper() {
        
        RowMapper<Map<String, Object>> rowMapper = new RowMapper<Map<String,Object>>() {
            
            /* (non-Javadoc)
             * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
             */
            public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
                
                Map<String, Object> result = new WeakHashMap<String, Object>();
                
                result.put("organizationName", rs.getObject("organizationName"));
                result.put("vehicleCode", rs.getObject("vehicleCode"));
                result.put("vehicleBarCode", rs.getObject("vehicleBarCode"));
                result.put("vehicleFeatureName", rs.getObject("vehicleFeatureName"));
                result.put("teamName", rs.getObject("teamName"));
                result.put("distance", rs.getObject("distance"));
                
                return result;
                
            }
            
        };
        
        return rowMapper;
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return order;
    }

    /**
     * @return the organizationIds
     */
    public List<Long> getOrganizationIds() {
        return organizationIds;
    }

    /**
     * @return the code
     */
    public String getCode() {
        return code;
    }

    /**
     * @return the barCode
     */
    public String getBarCode() {
        return barCode;
    }

    /**
     * @return the vehicleFeatureId
     */
    public Long getVehicleFeatureId() {
        return vehicleFeatureId;
    }

    /**
     * @return the teamId
     */
    public Long getTeamId() {
        return teamId;
    }

    /**
     * @return the beginDistance
     */
    public BigDecimal getBeginDistance() {
        return beginDistance;
    }

    /**
     * @return the endDistance
     */
    public BigDecimal getEndDistance() {
        return endDistance;
    }

    /**
     * @return the limit
     */
    public Integer getLimit() {
        return limit;
    }

    /**
     * @param organizationIds the organizationIds to set
     */
    public void setOrganizationIds(List<Long> organizationIds) {
        this.organizationIds = organizationIds;
    }

    /**
     * @param code the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @param barCode the barCode to set
     */
    public void setBarCode(String barCode) {
        this.barCode = barCode;
    }

    /**
     * @param vehicleFeatureId the vehicleFeatureId to set
     */
    public void setVehicleFeatureId(Long vehicleFeatureId) {
        this.vehicleFeatureId = vehicleFeatureId;
    }

    /**
     * @param teamId the teamId to set
     */
    public void setTeamId(Long teamId) {
        this.teamId = teamId;
    }

    /**
     * @param beginDistance the beginDistance to set
     */
    public void setBeginDistance(BigDecimal beginDistance) {
        this.beginDistance = beginDistance;
    }

    /**
     * @param endDistance the endDistance to set
     */
    public void setEndDistance(BigDecimal endDistance) {
        this.endDistance = endDistance;
    }

    /**
     * @param limit the limit to set
     */
    public void setLimit(Integer limit) {
        this.limit = limit;
    }

}
